package dbhandler;

import evthandler.EventMessage;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;

/**
 *
 * @author Casper, Dennis, Jackie, Jakob
 * @version 1.00
 */
public class Room {

    private String roomNo;
    private boolean oceanView;
    private boolean executive;
    private boolean handicap;
    private Category category;
    private ArrayList<Date> dateList;

    /**
     * Empty constructor to make it possible to call methods from this class
     * without initializing a new room object with data.
     */
    public Room() {
    }

    /**
     * Constructor to create a room object with corresponding data to the
     * given room number.
     * 
     * @param roomNo
     */
    public Room(String roomNo) {
        dateList = new ArrayList<Date>();
        this.roomNo = roomNo;
        int categoryNo = 0;
        try {
            ResultSet rs = DbCon.getInstance().executeQuery("SELECT * FROM room WHERE roomNo='" + roomNo + "'");
            while (rs.next()) {
                oceanView = rs.getBoolean("oceanView");
                executive = rs.getBoolean("executive");
                handicap = rs.getBoolean("handicap");
                categoryNo = rs.getInt("categoryNo");
            }
        } catch (SQLException ex) {
            new EventMessage("SQL error in Room", "There was an error performing the sql inquery\n").showException(ex);
        }
        category = new Category(categoryNo);
    }

    /**
     * Method to create Date ArrayLists for every room there is in the DB.
     * @return ArrayList of Dates.
     */
    public ArrayList<Date> getDateArray(){
        ArrayList<Date> dateArray = new ArrayList<Date>();
        ArrayList<String> inDate = new ArrayList<String>();
        ArrayList<String> outDate = new ArrayList<String>();
        try {
            ResultSet rs = DbCon.getInstance().executeQuery("SELECT inDate, outDate FROM booking WHERE roomNo='"+roomNo+"'");
            while (rs.next()) {
                inDate.add(rs.getString("inDate"));
                outDate.add(rs.getString("outDate"));
            }
        } catch (SQLException ex) {
            new EventMessage("SQL error in Room.createDateArray()", "There was an error performing the sql inquery\n").showException(ex);
        }
        Booking booking = new Booking();
        
        for (int i = 0; i < inDate.size(); i++) {
            String inDateTemp = null;
            String outDateTemp = null;
            ArrayList<Date> tempDate = new ArrayList<Date>();

            inDateTemp = inDate.get(i);
            outDateTemp = outDate.get(i);
            tempDate = booking.getIntermediateDays(inDateTemp, outDateTemp);

            for (int j = 0; j < tempDate.size(); j++) {
                Date dateTemp;
                dateTemp = tempDate.get(j);
                dateArray.add(dateTemp);
            }
        }
        return dateArray;
    }

    /**
     * Accessor to get the room number.
     * @return String roomNo
     */
    public String getRoomNo() {
        return roomNo;
    }

    /**
     * Method to create an ArrayList of Room objects for every room there is in
     * the DB.
     * @return ArrayList of Rooms
     */
    public ArrayList<Room> getAllRooms() {
        ArrayList<Room> allRooms = new ArrayList<Room>();
        ArrayList<String> temp = new ArrayList<String>();
        try {
            ResultSet rs = DbCon.getInstance().executeQuery("SELECT roomNo FROM room");
            while (rs.next()) {
                temp.add(rs.getString("roomNo"));
            }
        } catch (SQLException ex) {
            new EventMessage("SQL error in Room.getAllRooms()", "There was an error performing the sql inquery\n").showException(ex);
        }
        for (int i = 0; i < temp.size(); i++) {
            allRooms.add(new Room(temp.get(i)));
        }
        return allRooms;
    }

    /**
     * Mutator to set the global variable datelist to another.
     * @param datelist
     */
    public void setDateList(ArrayList<Date> datelist){
        this.dateList = datelist;
    }

    /**
     * Accessor to get the ArrayList of Date objects.
     * @return ArrayList of Date objects.
     */
    public ArrayList<Date> getDateList(){
        return dateList;
    }

    /**
     * Accessor to get the accessible boolean.
     * @return boolean
     */
    public boolean isHandicap(){
        return handicap;
    }

    /**
     * Accessor to get the category object.
     * @return Category
     */
    public Category getCategory() {
        return category;
    }

    /**
     * Accessor to get the executive boolean.
     * @return boolean
     */
    public boolean isExecutive() {
        return executive;
    }

    /**
     * Accessor to get the oceanview boolean.
     * @return boolean
     */
    public boolean isOceanView() {
        return oceanView;
    }
}
